

const { Spot} = require('@binance/connector');

import tunnel from 'tunnel';
import config from '../config';


class BinaClient {
    isProxy: boolean;
    apiKey?: string;
    apiSecret?: string;
    client:any;
    constructor(isProxy:boolean) {
        this.isProxy = isProxy;

    }
    async createClient() {
        if(this.isProxy) {

            const agent = tunnel.httpsOverHttp({
                proxy:config.bina.proxy
            })
            this.client = new Spot(this.apiKey, this.apiSecret, {
                baseURL: "https://api.binance.com",
                httpsAgent: agent,
                timeout: config.bina.timeOut })
        }else{

            this.client = new Spot(this.apiKey, this.apiSecret, {
                timeout: config.bina.timeOut })
        }
    }
    async getKline(symbole:string, interval:string, startTime:number, endTime:number, limit:number):Promise<any> {
        try{
            let ret = await this.client.klines(symbole, interval, {startTime, endTime, limit});
            if (ret && ret.data) {
                return ret.data
            } else {
                return [];
            }
        }catch (e) {
            console.log('getKline error: ', e);
            return [];
        }

    }
}

export default BinaClient;